package org.javaboy.demo.controller;

import jakarta.servlet.http.HttpServletRequest;
import org.javaboy.demo.model.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

//@Controller
//@ResponseBody
@RestController
public class UserController {


    /**
     * http://localhost:8080/s/user?id=1
     * http://localhost:8080/s/user/1
     * @return
     */
    @GetMapping("/user/{id}")
    public User getUserById(@PathVariable("id") Integer idx) {
        User user = new User();
        user.setId(idx);
        return user;
    }

    @PostMapping("/user")
    public User addUser(@RequestBody User user) {
        return user;
    }
    @PutMapping("/user")
    public User updateUser(@RequestBody User user) {
        return user;
    }

    @DeleteMapping("/user/{id}")
    public void deleteById(@PathVariable Integer id) {
        System.out.println("id = " + id);
    }

    public void test01(HttpServletRequest request) {
        //http://localhost:8080/s/user/1
        //获取请求路径的信息 /1
        String pathInfo = request.getPathInfo();
        String scheme = request.getScheme();//http
        String serverName = request.getServerName();//localhost
        int serverPort = request.getServerPort();//8080
        String contextPath = request.getContextPath();// /s
        String servletPath = request.getServletPath(); // /user

    }
}
